package com.example.baseproject.easyhttptest;

import androidx.annotation.NonNull;

import com.hjq.http.annotation.HttpHeader;
import com.hjq.http.annotation.HttpIgnore;
import com.hjq.http.annotation.HttpRename;
import com.hjq.http.config.IRequestApi;
import com.hjq.http.config.IRequestBodyStrategy;
import com.hjq.http.config.IRequestCache;
import com.hjq.http.config.IRequestClient;
import com.hjq.http.config.IRequestInterceptor;
import com.hjq.http.config.IRequestServer;
import com.hjq.http.config.IRequestType;
import com.hjq.http.model.CacheMode;
import com.hjq.http.model.HttpHeaders;
import com.hjq.http.model.HttpParams;
import com.hjq.http.model.RequestBodyType;
import com.hjq.http.request.HttpRequest;
import com.tjcd.project.library_base.base.network.SSLSocketClient;

import java.io.File;
import java.io.InputStream;
import java.util.List;
import java.util.concurrent.TimeUnit;

import lombok.Data;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * @Author jiangKunKun
 * @CreateDate 2024/9/27
 * @Description
 * @SpittingGrooves
 */
@Data
public class TestApi implements IRequestServer, IRequestApi,
        IRequestType, IRequestCache, IRequestClient, IRequestInterceptor {
    @NonNull
    @Override
    public String getApi() {
        /**
         * IRequestServer  包含   IRequestHost, IRequestClient,
         *         IRequestType, IRequestCache
         * 多base
         * https://www.baidu.com/
         * 或者 集成某个第二经常用 XxxServer
         * public final class XxxApi extends XxxServer
         */
        // 重定向
        return "";
    }

    @NonNull
    @Override
    public String getHost() {
        /**
         * IRequestApi
         * 后半部分接口链接
         * 可拼接 动态变化
         * login/password/test
         */
//        return "article/query/" + pageNumber + "/json"; 拼接参数
        return "user/getInfo";
    }
    @NonNull
    @Override
    public CacheMode getCacheMode() {
        /**
         * IRequestCache
         * 设置优先使用缓存
         */
        return CacheMode.USE_CACHE_FIRST;
    }

    /**
     * 忽略参数 HttpIgnore
     */
    @HttpIgnore
    private String token;
    /**
     * 传入请求头
     */
    @HttpHeader
    private String time;
    /**
     * 重命名参数或者请求头的名称
     */
    @HttpRename("k")
    private String keyword;

//    @NonNull
//    @Override
//    public BodyType getBodyType() {
//        /**
//         * IRequestType
//         * BodyType.FORM 上传文件需要使用表单的形式提交
//         */
//        return BodyType.JSON;
//    }

    /**
     * RequestBodyType.JSON;
     * RequestBodyType.FORM;
     * @return
     */
    @NonNull
    @Override
    public IRequestBodyStrategy getBodyType() {
        return RequestBodyType.JSON;
    }

    /** 本地图片或文件 */
    private File image;
    private List<File> files;
    /**
     * InputStream 对象上传
     */
    private InputStream inputStream;
    /**
     * RequestBody 对象上传
     */
    private RequestBody requestBody;

    @NonNull
    @Override
    public OkHttpClient getOkHttpClient() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        // 使用自定义的 SSLSocketFactory 和 X509TrustManager
        builder.sslSocketFactory(SSLSocketClient.getSSLSocketFactory(), SSLSocketClient.getTrustManager());
        builder.hostnameVerifier(SSLSocketClient.getHostnameVerifier()); // 取消验证主机名
        builder.connectTimeout(30, TimeUnit.SECONDS); // 连接超时的时间
        builder.readTimeout(30, TimeUnit.SECONDS); // 连接的读取超时时间
        builder.writeTimeout(30, TimeUnit.SECONDS); // 写入超时时间
//        builder.addInterceptor(new NetInterceptor()); // 添加拦截器
        return builder.build();
    }

    /**
     * 自定义请求头中的 ContentType
     */
    @HttpHeader
    @HttpRename("Content-Type")
    private String contentType = "application/x-www-form-urlencoded;charset=utf-8";

    @Override
    public void interceptArguments(@NonNull HttpRequest<?> httpRequest, @NonNull HttpParams params, @NonNull HttpHeaders headers) {
        IRequestInterceptor.super.interceptArguments(httpRequest, params, headers);
    }

    @NonNull
    @Override
    public Request interceptRequest(@NonNull HttpRequest<?> httpRequest, @NonNull Request request) {
        return IRequestInterceptor.super.interceptRequest(httpRequest, request);
    }

    @NonNull
    @Override
    public Response interceptResponse(HttpRequest<?> httpRequest, Response response) {
        return IRequestInterceptor.super.interceptResponse(httpRequest, response);
    }

    public static void main(String[] args) {
//        // 根据 TAG 取消请求任务
//        EasyHttp.cancel("tag");
//        // 取消指定 Tag 标记的请求
//        EasyHttp.cancel("tag");
//        // 取消所有请求
//        EasyHttp.cancel();

//        EasyHttp.post(MainActivity.this)
//                .api(new TestApi())
//                // 延迟 5 秒后请求
//                .delay(5000)
//                .tag("abc")
//                .request(new HttpCallbackProxy<HttpData<XxxBean>>(MainActivity.this) {
//
//                    @Override
//                    public void onHttpSuccess(HttpData<XxxBean> result) {
//
//                    }
//                });
    }
}
